package com.example.meitu.api.service.impl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.meitu.api.domain.DictItem;
import com.example.meitu.api.service.DictItemService;
import com.example.meitu.api.dao.DictItemMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 蔡超
* @description 针对表【sys_dict_item(字典项)】的数据库操作Service实现
* @createDate 2024-06-26 10:21:54
*/
@Service
public class DictItemServiceImpl extends ServiceImpl<DictItemMapper, DictItem>
    implements DictItemService{

    @Override
    @Cacheable(value="dict", key = "#code")
    public List<String> listByType(String code) {
        LambdaQueryWrapper<DictItem> queryWrapper = new LambdaQueryWrapper<DictItem>();
        queryWrapper.eq(DictItem::getDictType, code);
        queryWrapper.eq(DictItem::getDelFlag,'0');
        queryWrapper.orderByDesc(DictItem::getSortOrder);
        List<DictItem> list = list(queryWrapper);
        List<String> result = list.stream().map(DictItem::getItemValue).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<JSON> listJson(String code) {
        LambdaQueryWrapper<DictItem> queryWrapper = new LambdaQueryWrapper<DictItem>();
        queryWrapper.eq(DictItem::getDictType, code);
        queryWrapper.eq(DictItem::getDelFlag,'0');
        queryWrapper.orderByDesc(DictItem::getSortOrder);
        List<DictItem> list = list(queryWrapper);
        List<JSON> result = new ArrayList<>();
        for (DictItem item : list) {
            JSON json = JSONUtil.parseObj(item.getItemValue());
            result.add(json);
        }
        return result;
    }
}




